# coding=utf-8
from string import Template
from generator.libsomeip.some_ip_type import  SomeIP

from utils.util import  removeDir, writeFile

SomeIP_proto = Template(
"""syntax = "proto2";
option optimize_for = LITE_RUNTIME;

package proto.someip;

${messageList}

""")

message_template=Template(
"""
message ${name} {
${content}
}
"""
)

Android_bp=Template(
"""
cc_library {
    name: "libsomeip-proto",
    proprietary: true,
    proto: {
        export_proto_headers: true,
        type: "lite",
    },
    strip: {
        keep_symbols: true,
    },
    srcs: ["someip.proto"]
}

java_library {
    name: "libsomeip-proto-java",
    proto: {
        type: "nano",
    },
    srcs: ["someip.proto"],
    product_variables: {
        pdk: {
            enabled: false,
        },
    },
}

"""
)


def gen_SomeIPProtoBuf(someip:SomeIP):
    #整理数据,以serviceId划分一组message,并只需要有透传的
    messageList=[]
    for msg in someip.msg:
        for item in [item for item in msg.item if item.isDirectTransmit()]:
            encodeType = item.encodeType
            if encodeType is not None and encodeType.isStruct():
                messageList.append(message_template.substitute(
                    name = encodeType.getTypeName(),
                    content="\n".expandtabs(4).join(encodeType.toProtpBufferContent())
                ))
    writeFile("./data/transfer/libsomeip/proto/", "someip.proto",lambda file:file.write(SomeIP_proto.substitute(
        messageList="".join(messageList)
    )))
    writeFile("./data/transfer/libsomeip/proto/", "Android.bp.in",lambda file:file.write(Android_bp.substitute()))

